package com.atguigu.gmall.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: dongdong lau
 * @CreateTime: 2021/7/16 18:26
 * @Description: 秒杀rabbitMQ配置类:交换机和队列以及绑定
 */
@Configuration
public class SeckillRabbitConfig {

    /**
     * 秒杀交换机
     * @return
     */
    @Bean("seckillExchange")
    public Exchange seckillExchange(){
        return ExchangeBuilder.topicExchange("seckill_exchange").build();
    }

    /**
     * 秒杀队列
     * @return
     */
    @Bean("seckillQueue")
    public Queue seckillQueue(){
        return QueueBuilder.durable("seckill_queue").build();
    }

    /**
     * 队列与交换机进行绑定
     * @param seckillExchange
     * @param seckillQueue
     * @return
     */
    @Bean
    public Binding seckillBinding(@Qualifier("seckillExchange") Exchange seckillExchange,
                                  @Qualifier("seckillQueue") Queue seckillQueue){
        return BindingBuilder
                .bind(seckillQueue)
                .to(seckillExchange)
                .with("seckill.#")
                .noargs();
    }
}
